package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.INoticeService;
import com.igeekhome.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;
    @RequestMapping("/no")
    public String no(Model model, HttpSession session, @RequestParam("value")String value,@RequestParam("current")Integer current, @RequestParam("size")Integer size){


        IPage<Notice> page =new Page<>(current,size);
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        qw.like("title",value).or().like("content",value).or().like("creatorid",value);
        IPage<Notice> page1=this.iNoticeService.page(page,qw);
        List<Notice> list = page1.getRecords();
        session.setAttribute("notice",this.iNoticeService.count());
        long pagesCount=page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);

        return "notice";
    }

    @RequestMapping("/addsuccess")
    public String addwl(Notice notice){
        LocalDateTime newLDT = LocalDateTime.now();

        int year = newLDT.getYear();//年
        int month = newLDT.getMonth().getValue();//月
        int dayOfMonth = newLDT.getDayOfMonth();//日
        int hour = newLDT.getHour();//时
        int minute = newLDT.getMinute();//分
        int second = newLDT.getSecond();//


        Notice n = new Notice(notice.getTitle(),notice.getContent(),notice.getCreatorid(),newLDT);
        System.out.println("添加操作成功");
        this.iNoticeService.save(n);

        return "redirect:/notice/no?current=1&size=6";

    }

}

